<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>公益中心管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="公益中心名称">
					<el-input v-model="queryInfo.commonwealTitle" @clear="queryList" placeholder="请输
入公益中心名称" clearable=""></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddcommonweal">新增公益中心</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="commonwealList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="commonwealId" label="ID"></el-table-column>
			<el-table-column prop="commonwealTitle" label="标题">
			</el-table-column>
			<el-table-column prop="commonwealIntroduction" label="简介">
			</el-table-column>
			<el-table-column prop="commonwealListChart" label="列表图">
				<template slot-scope='scope'>
					<el-image style="width: 100px;
					height: 100px" :src="url+'download?filename='+scope.row.commonwealListChart"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="commonwealDetails" label="详情内容">
			</el-table-column>
			<el-table-column prop="commonwealCreateTime" label="创建时间">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditcommonweal(scope.row)">编辑</el-link>
					<el-link type="primary" @click="toDeletecommonweal(scope.row.commonwealId)">删除</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 15]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>


		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="commonweal">
				<el-form-item label="标题" label-width="80px">
					<el-input placeholder="请输入标题" v-model="commonweal.commonwealTitle"></el-input>
				</el-form-item>
				<el-form-item label="简介" label-width="80px">
					<el-input placeholder="请输入简介" v-model="commonweal.commonwealIntroduction"></el-input>
				</el-form-item>

				<el-form-item label="列表图" label-width="80px">
					<!-- <el-input placeholder="请输入公益中心图片" v-model="commonweal.commonwealImage"></el-input> -->
					<!-- 
								 el-upload:上传组件
									action： 上传网址
									list-type：上传组件类型
									on-preview ： 图片预览事件
									on-remove ：移除图片事件
									el-dialog : 预览图片组件
									on-success : 上传成功后的回调函数
									limit  ： 限制上传图片个数
									fileList:就是用来展示图片的路径的数组
							-->
					<el-upload :action="url+'upload'" list-type="picture-card" :on-success="handleSuccess" :limit="1"
						:on-preview="handlePictureCardPreview" :on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
				<el-form-item label="详细内容" label-width="80px">
					<!-- <el-input placeholder="请输入公益中心详情" v-model="commonweal.commonwealDetails">	</el-input> -->
					<!-- 富文本编辑器组件 -->
					<quill-editor v-model="commonweal.commonwealDetails"></quill-editor>

				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addcommonweal">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		url
	} from '../../util/tool.js'
	export default {
		data() {
			return {

				url: '', // 定义一个全局的url
				// 查询信息对象
				queryInfo: {
					commonwealTitle: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 0,
				commonwealList: [],
				title: '',
				dialogFormVisible: false,
				commonweal: { // 公益中心对象
					"commonwealId": '',
					"commonwealTitle": "",
					"commonwealIntroduction": "",
					"commonwealListChart": "",
					"commonwealDetails": "",
					"commonwealCreateTime": ""
				},
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
				fileList: []
			}
		},
		methods: {
			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径，交给公益中心对象的commonwealListChart
				this.commonweal.commonwealListChart = response.message;
			},

			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				// 移除数据
				this.commonweal.commonwealListChart = '';
			},

			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			cancle() {
				this.commonweal = { // 公益中心对象
					"commonwealId": '',
					"commonwealTitle": "",
					"commonwealIntroduction": "",
					"commonwealListChart": "",
					"commonwealDetails": "",
					"commonwealCreateTime": ""
				}
				// 关闭对话框
				this.dialogFormVisible = false;
				// 重新查询
				this.queryList()
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},
			//	去删除公益中心内容
			toDeletecommonweal(commonwealId) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('commonweal/' + commonwealId)
							.then(res => {
								if (res.data.status == 200) {
									this.$message.success("删除成功")
									this.queryList()
								} else {
									this.$message.error("删除失败")
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			//	去修改公益中心内容
			toEditcommonweal(row) {
				console.log(row.commonwealListChart);
				// 赋值
				this.commonweal = row;
				// 设置标题
				this.title = "修改公益内容";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				this.fileList = [{
					name: row.commonwealListChart,
					url: this.url + 'download?filename=' + row.commonwealListChart
				}];
			},

			//	添加或修改公益中心内容
			addcommonweal() {
				if (this.commonweal.commonwealId) { //	修改	true
					this.$http.put("commonweal", this.commonweal)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("修改成功！")
								// 清空数据
								this.commonweal = { // 公益中心对象
									"commonwealId": '',
									"commonwealTitle": "",
									"commonwealIntroduction": "",
									"commonwealListChart": "",
									"commonwealDetails": "",
									"commonwealCreateTime": ""
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("修改失败！")
							}
						})
				} else { //	添加
					this.$http.post("commonweal", this.commonweal)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("添加成功！")
								// 清空数据
								this.commonweal = { // 公益中心对象
								"commonwealId": '',
								"commonwealTitle": "",
								"commonwealIntroduction": "",
								"commonwealListChart": "",
								"commonwealDetails": "",
								"commonwealCreateTime": ""
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("添加失败！")
							}
						})
				}
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			// 进入添加的对话框
			toAddcommonweal() {
				this.title = "新增公益内容";
				this.dialogFormVisible = true;

			},

			// 分页查询所有的数据
			queryList() {
				//	调用后端接口
				this.$http.get('commonweal', {
						params: this.queryInfo
					})
					.then(res => {
						if (res.data.status == 200) {
							console.log(res);
							this.commonwealList = res.data.data.list
							this.total = res.data.data.total
						} else {

						}
					})
			},
			handleSizeChange(size) {
				// console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size
				this.queryInfo.pageNum = 1
				this.queryList()
			},
			handleCurrentChange(num) {
				// console.log('页码发生改变');
				this.queryInfo.pageNum = num
				this.queryList()
			}
		},
		mounted() {
			this.queryList()
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			this.url = url;
		}
	}
</script>
<style>
	/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
	.ql-container {
		min-height: 200px;
	}

	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>